package com.workstudy.wstudy.controller;

import java.util.Date;
import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.system.service.ISysUserService;
import com.workstudy.wstudy.domain.Constants;
import com.workstudy.wstudy.domain.WstApply;
import com.workstudy.wstudy.domain.WstPostUser;
import com.workstudy.wstudy.mapper.WstPostUserMapper;
import com.workstudy.wstudy.service.IWstApplyService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.wstudy.domain.WstPost;
import com.workstudy.wstudy.service.IWstPostService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 岗位管理Controller
 * 
 * @author sun
 * @date 2021-04-04
 */
@Controller
@RequestMapping("/wstudy/post")
public class WstPostController extends BaseController
{
    private String prefix = "wstudy/post";

    @Autowired
    private IWstPostService wstPostService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private WstPostUserMapper wstPostUserService;

    @Autowired
    private IWstApplyService applyService;

    @RequiresPermissions("wstudy:post:view")
    @GetMapping()
    public String post(ModelMap modelMap)
    {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        modelMap.put("user",user);
        return prefix + "/post";
    }

    /**
     * 查询岗位管理列表
     */
    @RequiresPermissions("wstudy:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WstPost wstPost)
    {
        startPage();
        List<WstPost> list = wstPostService.selectWstPostList(wstPost);
        return getDataTable(list);
    }

    /**
     * 导出岗位管理列表
     */
    @RequiresPermissions("wstudy:post:export")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WstPost wstPost)
    {
        List<WstPost> list = wstPostService.selectWstPostList(wstPost);
        ExcelUtil<WstPost> util = new ExcelUtil<WstPost>(WstPost.class);
        return util.exportExcel(list, "post");
    }

    /**
     * 新增岗位管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位管理
     */
    @RequiresPermissions("wstudy:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WstPost wstPost)
    {
        return toAjax(wstPostService.insertWstPost(wstPost));
    }

    /**
     * 修改岗位管理
     */
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap mmap)
    {
        WstPost wstPost = wstPostService.selectWstPostById(postId);
        mmap.put("wstPost", wstPost);
        return prefix + "/edit";
    }

    /**
     * 岗位管理 详情
     */
    @GetMapping("/details/{postId}")
    public String details(@PathVariable("postId") Long postId, ModelMap mmap)
    {
        WstPost wstPost = wstPostService.selectWstPostById(postId);
        mmap.put("wstPost", wstPost);
        return prefix + "/details";
    }


    /**
     * 岗位管理 详情
     */
    @GetMapping("/postStudent")
    public String postStudent(ModelMap mmap)
    {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        mmap.put("userId", user.getUserId());
        return prefix + "/post_student";
    }

    /**
     * 岗位管理 审核人员
     */
    @GetMapping("/examineUsers/{roleKey}")
    public String details(@PathVariable("roleKey") String roleKey,ModelMap mmap)
    {
        List<SysUser> users = userService.getUsersByRoleKey(roleKey);
        mmap.put("users",users);
        return prefix + "/examineUsers";
    }

    /**
     * 岗位管理 审核
     */
    @GetMapping("/examinePage/{postId}")
    public String examinePage(@PathVariable("postId") Long postId, ModelMap mmap)
    {
        WstPost wstPost = wstPostService.selectWstPostById(postId);
        mmap.put("wstPost", wstPost);
        return prefix + "/examine_post";
    }

    /**
     * 修改保存岗位管理
     */
    @RequiresPermissions("wstudy:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WstPost wstPost)
    {
        return toAjax(wstPostService.updateWstPost(wstPost));
    }

    /**
     * 删除岗位管理
     */
    @RequiresPermissions("wstudy:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wstPostService.deleteWstPostByIds(ids));
    }


    /**
     * 岗位申请页面
     * @return
     */
    @GetMapping("/post_user")
    public String postUserPage(ModelMap modelMap)
    {
        SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
        WstPostUser postUser = new WstPostUser();
        WstApply paramsApply = new WstApply();
        paramsApply.setStudentId(user.getUserId());
        postUser.setUserId(user.getUserId());
        List postUsers =  wstPostUserService.selectWstPostUserList(postUser);
        List<WstApply> applies = applyService.selectWstApplyList(paramsApply);
        modelMap.put("postUsers",postUsers);
        modelMap.put("applies",applies);
        return prefix + "/post_user";
    }

    /**
     * 岗位申请数据
     * @return
     */
    @PostMapping("/postUserData")
    @ResponseBody
    public TableDataInfo postUserData(WstPost wstPost)
    {
        startPage();
        wstPost.setStatus(Constants.PostStatus.IN_HANDING.toString());
        wstPost.setStartTime(new Date());
        wstPost.setEndTime(new Date());
        List<WstPost> list = wstPostService.selectWstPostList(wstPost);
        return getDataTable(list);
    }
}
